package com.cup.bddm.impala.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.cup.bddm.impala.ImpalaJdbc;
import com.cup.bddm.impala.po.User;

@Repository("UserDao")
public class UserDao {

	static Logger logger = LoggerFactory.getLogger(UserDao.class);
	@Autowired
	private ImpalaJdbc impalaJdbc;

	public User getUserInfoByNameAndInsCd(String userName, String insCd) {

		String sql = "SELECT user_id, login_name, user_name,user_pwd,cup_branch_ins_id_cd,cup_branch_nm,role_id,sub_ins_nm " + "FROM tbl_qrc_user_info where login_name=? and cup_branch_ins_id_cd=?";

		User user = null;
		try {
			Map<String, Object> userMap = impalaJdbc.queryForMap(sql, userName, insCd);

			if (userMap != null) {
				user = new User();
				user.setUser_id(userMap.get("user_id").toString());
				user.setLogin_name((String) userMap.get("login_name"));
				user.setUser_nm((String) userMap.get("user_name"));
				user.setUser_pwd((String) userMap.get("user_pwd"));
				user.setCup_branch_ins_id_cd((String) userMap.get("cup_branch_ins_id_cd"));
				user.setCup_branch_nm((String) userMap.get("cup_branch_nm"));
				user.setRole_id(userMap.get("role_id").toString());
				user.setSub_ins_nm((String) userMap.get("sub_ins_nm"));
			}

		} catch (SQLException e) {
			logger.error("error in query user info", e);
			throw new RuntimeException("error in query user info");
		}

		return user;
	}

	public boolean changePwd(String userId, String pwd) {
		String sql = "insert into tbl_qrc_user_info(user_id,rec_upd_ts,user_pwd) values(?,now(),?);";
		try {
			return impalaJdbc.execute(sql, userId, pwd);
		} catch (SQLException e) {
			logger.error("error in query user info", e);
			throw new RuntimeException("error in changge user pwd");
		}

	}

	public boolean addUserInfo(User user) {
		String sql = "insert into tbl_qrc_user_info(user_id,cup_branch_ins_id_cd,cup_branch_nm,login_name,role_id,sub_ins_nm,user_name,user_pwd,rec_upd_ts,rec_crt_ts) values(?,?,?,?,?,?,?,?,now(),now());";

		Connection conn = null;
		PreparedStatement stmt = null;
		try {
			conn = impalaJdbc.getConnection();
			stmt = conn.prepareStatement(sql);

			stmt.setString(1, user.getCup_branch_ins_id_cd() + "_" + user.getLogin_name());
			stmt.setString(2, user.getCup_branch_ins_id_cd());
			stmt.setString(3, user.getCup_branch_nm());
			stmt.setString(4, user.getLogin_name());
			stmt.setInt(5, Integer.valueOf(user.getRole_id()));
			stmt.setString(6, user.getSub_ins_nm() == null ? "" : user.getSub_ins_nm());
			stmt.setString(7, user.getUser_nm() == null ? "" : user.getUser_nm());
			stmt.setString(8, user.getUser_pwd());

			logger.info("register user : excute sql=" + sql);
			logger.info("user_id=" + user.getCup_branch_ins_id_cd() + "_" + user.getLogin_name() + ".cup_branch_ins_id_cd=" + user.getCup_branch_ins_id_cd() + ".cup_branch_nm=" + user.getCup_branch_nm() + ".login_name=" + user.getLogin_name() + ".role_id=" + user.getRole_id() + ".sub_ins_nm=" + user.getSub_ins_nm() + ".user_name=" + user.getUser_nm() + ".user_pwd" + user.getUser_pwd());

			stmt.execute();
		} catch (SQLException e) {
			logger.error("error in query user info", e);
			throw new RuntimeException("error in changge user pwd");
		} catch (Exception e) {
			logger.error("error in insert user info", e);
			return false;
		} finally {
			impalaJdbc.close(stmt);
			impalaJdbc.close(conn);
		}

		return true;

	}
}
